﻿using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;//cangxianEntities
using System.Text;
using System.Threading.Tasks;

namespace Freshbox2._0.DAL
{
    public class BaseDal<T> where T : class
    {
        Model1Container1 dbContext = new Model1Container1();

        //添加对 using System.Data.Entity;的引用  以及项目文件packages包里面EntityFramework.dll 的引用

        public int Add(T model)
        {
            dbContext.Set<T>().Add(model);
            return dbContext.SaveChanges();
        }

        public int Delete(T model)
        {
            dbContext.Entry(model).State = EntityState.Deleted;
            return dbContext.SaveChanges();
        }


        public int Delete(Func<T, bool> condition)
        {
            var list = dbContext.Set<T>().Where(condition).ToList();
            foreach (var item in list)
            {
                dbContext.Entry(item).State = EntityState.Deleted;
            }
            return dbContext.SaveChanges();
        }



        public int Modify(T model)
        {
            dbContext.Entry(model).State = EntityState.Modified;
            return dbContext.SaveChanges();
        }



        public IQueryable<T> Search()
        {
            return dbContext.Set<T>().AsQueryable();
        }


        public IQueryable<T> Search(Expression<Func<T, bool>> condition)
        {
            // return dbContext.Set<T>().AsNoTracking().Where(condition).AsQueryable();
            return dbContext.Set<T>().Where(condition).AsQueryable();
        }


        public IQueryable<T> Search<S>(Expression<Func<T, bool>> condition, Expression<Func<T, S>> order, bool isAsc)
        {
            if (isAsc)
            {
                return dbContext.Set<T>().Where(condition).OrderBy(order).AsQueryable();
            }
            else
            {
                return dbContext.Set<T>().Where(condition).OrderByDescending(order).AsQueryable();
            }
        }

        public IQueryable<T> Search<S>(Expression<Func<T, bool>> condition, bool isAsc, Expression<Func<T, S>> order, int pageSize, int pageIndex, out int count)
        {
            count = dbContext.Set<T>().Where(condition).Count();
            if (isAsc)
            {
                return dbContext.Set<T>().Where(condition).OrderBy(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            }
            else
            {
                return dbContext.Set<T>().Where(condition).OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            }
        }

        public IQueryable<T> Search<S>(IQueryable<T> query, bool isAsc, Expression<Func<T, S>> order, int pageSize, int pageIndex, out int count)
        {
            count = query.Count();
            if (isAsc)
            {
                return query.OrderBy(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            }
            else
            {
                return query.OrderByDescending(order).Skip((pageIndex - 1) * pageSize).Take(pageSize).AsQueryable();
            }
        }


        //如果使用db.Database.SqlQuery<T>("sql语句")进行分页查询的话，要注意避免内存分页。
        //错误的写法：内存分页
        //db.Database.SqlQuery<T>("select * from table").OrderByDescending(c => c.CreateTime).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();

        public IQueryable<S> SearchBySQL<S>(string sql)
        {
            return dbContext.Database.SqlQuery<S>(sql).AsQueryable();
        }





    }
}
